package logic

import (
	"VipSrv/global/model"
	"context"
	"fmt"
	"time"

	"VipSrv/internal/svc"
	"VipSrv/vip"

	"github.com/zeromicro/go-zero/core/logx"
)

type VipReturnLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVipReturnLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VipReturnLogic {
	return &VipReturnLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VipReturnLogic) VipReturn(in *vip.VipReturnRequest) (*vip.VipReturnResponse, error) {
	// todo: add your logic here and delete this line
	var Vip model.Vip
	fmt.Println(1)
	if err := model.DB.Where("snow = ?", in.OrderNo).Find(&Vip).Error; err != nil {
		return nil, err
	}
	Vip.VipStatus = in.Status
	if in.Status == "TRADE_SUCCESS" {
		var User model.User
		model.DB.Where("id = ?", Vip.UserId).Find(&User)
		if User.VipExpire.Unix() > time.Now().Unix() {
			User.VipExpire = User.VipExpire.AddDate(0, Vip.Month, 0)
		} else {
			User.VipExpire = time.Now().AddDate(0, Vip.Month, 0)
		}
		model.DB.Updates(&User)
	}
	model.DB.Updates(&Vip)
	return &vip.VipReturnResponse{}, nil
}
